package com.lanyou.esb.web.serviceMonitor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.util.DateUtil;
import com.lanyou.esb.entity.IfAlarmHis;
import com.lanyou.esb.repository.IfAlarmHisRepository;
import com.lanyou.esb.web.param.InterfaceWarningParam;
/**
 * 
 * @author ly-zengzugen
 *
 */


@RestController
@RequestMapping(Constants.BASE_URL_API + "/interfaceWarningHises")
public class InterfaceWarningHisController {

	@Autowired
	private IfAlarmHisRepository interfaceWarningHisRepository;
	/**
	 * 异常错误码设置
	 * @param param 类型:String 
	 * @param pageable 类型:String
	 * @return
	 */
	@RequestMapping("/search/findByExample")
	public Page<IfAlarmHis> findByExample(InterfaceWarningParam param, Pageable pageable) {
		
		Specification<IfAlarmHis> spec = new Specification<IfAlarmHis>() {
			@Override
			public Predicate toPredicate(Root<IfAlarmHis> root, CriteriaQuery<?> query, CriteriaBuilder cb) {				List<Predicate> predicatesList = new ArrayList<>();
				if(StringUtils.isNotBlank(param.getFieldName())){
					predicatesList.add(cb.like(root.get("fieldSystem").get("field").get("name"),param.getFieldName()));
				}
				
				if(StringUtils.isNotBlank(param.getFieldSystemName())){
					predicatesList.add(cb.like(root.get("fieldSystem").get("name"),  param.getFieldSystemName() ));
				}
				
				if(StringUtils.isNotBlank(param.getInterfaceName())){
					predicatesList.add(cb.like(root.get("interfaceName"), '%' +  param.getInterfaceName() + '%'));
				}
				
				if(StringUtils.isNotBlank(param.getAlarmType())){
					predicatesList.add(cb.like(root.get("alarmType"),  param.getAlarmType()));
				}
				
				if(StringUtils.isNotBlank(param.getAlarmLevel())){
					predicatesList.add(cb.like(root.get("alarmLevel"),  param.getAlarmLevel()));
				}
				
				// 纠正错误的时间参数
				DynaBean fixedDates = DateUtil.fixBeginTimeAndEndTime(param.getBeginOfCreateTime(),
						param.getEndOfCreateTime());
				Date fixedBeginTime = (Date) fixedDates.get("begin");
				Date fixedEndTime = (Date) fixedDates.get("end");

				Path<Date> pathDate = root.get("errorDate");
				predicatesList.add(cb.or(cb.isNull(pathDate), cb.between(pathDate, fixedBeginTime, fixedEndTime)));
				
				query.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
				return null;
			}
		};
		return interfaceWarningHisRepository.findAll(spec, pageable);
	}
}